﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Demo.Helper;
using Demo.Page;
using MetroFramework.Forms;

namespace Demo
{
    public partial class Home : MetroForm
    {
        UserHelper helper = new UserHelper("");
        List<Entity.User> listuser = new List<Entity.User>();

        public Home()
        {
            InitializeComponent();
            helper.CheckSuperUser("user.pt", listuser);
        }

        /// <summary>
        /// 打开用户界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUser_Click(object sender, EventArgs e)
        {
            // 检查是否已存在User窗体，避免重复创建
            if (Application.OpenForms.OfType<UserForm>().Any())
            {
                var existingForm = Application.OpenForms.OfType<UserForm>().First();
                existingForm.BringToFront();
                return;
            }

            UserForm form = new UserForm();
            form.Location = new Point(0, 120);
            form.TopMost = true;
            form.Show(this); // 非模态显示

            // 标记窗体是否正在关闭
            bool isClosing = false;

            // 监听User窗体的关闭事件
            form.FormClosing += (s, args) =>
            {
                isClosing = true; // 窗体开始关闭时标记
            };

            form.Deactivate += (s, args) =>
            {
                // 关键判断：窗体未关闭、未被释放、可见、已激活过，且不在关闭过程中
                if (!isClosing && !form.IsDisposed && form.Visible)
                {
                    MessageBox.Show(
                        form,
                        "请先关闭当前用户管理页面再操作其他内容！",
                        "提示",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning
                    );
                    form.BringToFront();
                    form.Focus();
                }
            };
        }

        /// <summary>
        /// 打开上下料控制界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOperation_Click(object sender, EventArgs e)
        {
            // 检查是否已存在OperationForm窗体，避免重复创建
            if (Application.OpenForms.OfType<OperationForm>().Any())
            {
                var existingForm = Application.OpenForms.OfType<OperationForm>().First();
                existingForm.BringToFront();
                return;
            }

            OperationForm form = new OperationForm();
            form.Location = new Point(0, 120);
            form.TopMost = true;
            form.Show(this); // 非模态显示
        }

        /// <summary>
        /// 打开产品界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProduct_Click(object sender, EventArgs e)
        {
            // 检查是否已存在ProductForm窗体，避免重复创建
            if (Application.OpenForms.OfType<ProductForm>().Any())
            {
                var existingForm = Application.OpenForms.OfType<ProductForm>().First();
                existingForm.BringToFront();
                return;
            }

            ProductForm form = new ProductForm();
            form.Location = new Point(0, 120);
            form.TopMost = true;
            form.Show(this); // 非模态显示
        }

        /// <summary>
        /// 打开大屏界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void brnDisplay_Click(object sender, EventArgs e)
        {
            // 检查是否已存在DisplayForm窗体，避免重复创建
            if (Application.OpenForms.OfType<DisplayForm>().Any())
            {
                var existingForm = Application.OpenForms.OfType<DisplayForm>().First();
                existingForm.BringToFront();
                return;
            }

            DisplayForm form = new DisplayForm();
            form.Location = new Point(0, 120);
            form.TopMost = true;
            form.Show(this); // 非模态显示
        }
    }
}
